

package org.service.monitor.config;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.notify.AbstractEventNotifier;
import de.codecentric.boot.admin.notify.RemindingNotifier;


@Configuration
@EnableScheduling
public class NotifierConfig {
    private static final Logger log = LoggerFactory.getLogger(NotifierConfig.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private RemindingNotifier remindingNotifier;

    @Bean
    @Primary
    public RemindingNotifier remindingNotifier() {

        RemindingNotifier remindingNotifier = new RemindingNotifier(new AbstractEventNotifier() {

            @Override
            protected void doNotify(ClientApplicationEvent event) throws Exception {
                if (event instanceof ClientApplicationStatusChangedEvent) {
                    ClientApplicationStatusChangedEvent changedEvent = (ClientApplicationStatusChangedEvent)event;
                    log.info("Application {} ({}) is {}", event.getApplication().getName(),
                        event.getApplication().getId(), changedEvent.getTo().getStatus());
                    String text = String.format("应用:%s 服务ID:%s,服务ip:%s 状态改变为：[%s ---> %s]，时间：%s",
                        event.getApplication().getName(), event.getApplication().getId(),
                        event.getApplication().getHealthUrl(), changedEvent.getFrom().getStatus(),
                        changedEvent.getTo().getStatus(),
                        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(changedEvent.getTimestamp())));
                    log.warn(text);
//                    SimpleMailMessage message = new SimpleMailMessage();
//                    message.setFrom("372527813@qq.com");
//                    message.setTo("285579511@qq.com");
//                    message.setSubject(event.getApplication().getName() + "服务状态改变");
//                    message.setText(text);
//                    javaMailSender.send(message);
//                    StatusUpdater
                } else {
                    log.info("Application {} ({}) {}", event.getApplication().getName(), event.getApplication().getId(),
                        event.getType());
                }
            }
        });
        // 每5分钟就需要提醒一次，并不一定会提醒，有 RemindingNotifier 里面的状态进行决定
        remindingNotifier.setReminderPeriod(TimeUnit.MINUTES.toMillis(5));
        return remindingNotifier;
    }

    /**
     * 每隔一分钟检查还有那些需要进行提醒
     */
    @Scheduled(fixedRate = 1_000L)
    public void remind() {
        remindingNotifier.sendReminders();
    }
}
